
#include "app.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "app.h"
#include "My_F3_GPIO.h"
#include "My_F3_TIM.h"
#include "My_F3_USART.h"

My_USART serial_1(USART1,115200, GPIOB, GPIO_Pin_6, GPIO_Pin_7);
My_USART serial_2(USART2,115200, GPIOA, GPIO_Pin_2, GPIO_Pin_3);
My_USART serial_3(USART3,115200, GPIOB, GPIO_Pin_10, GPIO_Pin_11);


void StartTaskCreate(void)
{
    /* Create one task. */
    xTaskCreate(vAPPTaskMain, "main"   ,256,NULL,1,NULL);
    xTaskCreate(vSystemNormalLED, "main"   ,256,NULL,1,NULL);

}

char cnt = 0;
char Last_len = 0;
char ResvData[2][256] = {0};
char Data_Flag = 0;
char First_Resv = 0;
char reply[] = "6789";
void Resv_Data_test_1()
{
    if(First_Resv == 0)
    {
        serial_1.ReceiveData(ResvData[0],256);
        Data_Flag = 0;
        First_Resv = 1;
    }
    
    if(Last_len == serial_1.Get_RxSport_Num())
    {
        if(Last_len != 0)
        {
            cnt++;
        }
    }
    else
    {
        Last_len = serial_1.Get_RxSport_Num();
        
        cnt = 0;
    }
    
    if(cnt > 3||(Last_len >= (char)128))
    {
        Data_Flag ++;
        Data_Flag %= 2;
        
        serial_1.ReceiveData(ResvData[Data_Flag],256);
        
        char temp = (Data_Flag == 1)?0:1;
//        Decode_Protocol(ResvData[temp],Last_len);
				serial_1.SendData(ResvData[temp], Last_len);
        
        cnt = 0;
        Last_len = 0;
    }
}


char cnt_2 = 0;
char Last_len_2 = 0;
char ResvData_2[2][256] = {0};
char Data_Flag_2 = 0;
char First_Resv_2 = 0;
char reply_2[] = "6789";
void Resv_Data_test_2()
{
    if(First_Resv_2 == 0)
    {
        serial_2.ReceiveData(ResvData_2[0],256);
        Data_Flag_2 = 0;
        First_Resv_2 = 1;
    }
    
    if(Last_len_2 == serial_2.Get_RxSport_Num())
    {
        if(Last_len_2 != 0)
        {
            cnt_2++;
        }
    }
    else
    {
        Last_len_2 = serial_2.Get_RxSport_Num();
        
        cnt_2 = 0;
    }
    
    if(cnt_2 > 3||(Last_len_2 >= (char)128))
    {
        Data_Flag_2 ++;
        Data_Flag_2 %= 2;
        
        serial_2.ReceiveData(ResvData_2[Data_Flag_2],256);
        
        char temp_2 = (Data_Flag_2 == 1)?0:1;
//        Decode_Protocol(ResvData[temp],Last_len);
				serial_2.SendData(ResvData_2[temp_2], Last_len_2);
        
        cnt_2 = 0;
        Last_len_2 = 0;
    }
}


char cnt_3 = 0;
char Last_len_3 = 0;
char ResvData_3[2][256] = {0};
char Data_Flag_3 = 0;
char First_Resv_3 = 0;
char reply_3[] = "6789";
void Resv_Data_test_3()
{
    if(First_Resv_3 == 0)
    {
        serial_3.ReceiveData(ResvData_3[0],256);
        Data_Flag_3 = 0;
        First_Resv_3 = 1;
    }
    
    if(Last_len_3 == serial_3.Get_RxSport_Num())
    {
        if(Last_len_3 != 0)
        {
            cnt_3++;
        }
    }
    else
    {
        Last_len_3 = serial_3.Get_RxSport_Num();
        
        cnt_3 = 0;
    }
    
    if(cnt_3 > 3||(Last_len_3 >= (char)128))
    {
        Data_Flag_3 ++;
        Data_Flag_3 %= 2;
        
        serial_3.ReceiveData(ResvData_3[Data_Flag_3],256);
        
        char temp_3 = (Data_Flag_3 == 1)?0:1;
//        Decode_Protocol(ResvData[temp],Last_len);
				serial_3.SendData(ResvData_3[temp_3], Last_len_3);
        
        cnt_3 = 0;
        Last_len_3 = 0;
    }
}

void vAPPTaskMain(void *pvArg) 
{

	
		My_PWM pwm1(TIM1, 32, 6000);
		pwm1.Init_PWM(GPIOA, GPIO_Pin_8, TIM1, 1);
//		pwm1.Output(0.4f);
		pwm1.Output(0.999f);
	
			My_GPIO conIN1(GPIOA, GPIO_Pin_9);
			My_GPIO conIN2(GPIOA, GPIO_Pin_10);
			My_GPIO conENA(GPIOA, GPIO_Pin_11);
			conIN1.Set();
			conIN2.Reset();

//		My_PWM pwm2(TIM1, 32, 6000);
//		pwm2.Init_PWM(GPIOA, GPIO_Pin_9, TIM1, 2);
//		pwm2.Output(0.4f);
//	
//		My_PWM pwm3(TIM1, 32, 6000);
//		pwm3.Init_PWM(GPIOA, GPIO_Pin_10, TIM1, 3);
//		pwm3.Output(0.4f);
	
//		My_PWM pwma(TIM2, 32, 6000);
//		pwma.Init_PWM(GPIOA, GPIO_Pin_0, TIM2, 1);
//		pwma.Output(0.4f);

//		My_PWM pwmb(TIM2, 32, 6000);
//		pwmb.Init_PWM(GPIOA, GPIO_Pin_1, TIM2, 2);
//		pwmb.Output(0.4f);

//		My_PWM pwmc(TIM2, 32, 6000);
//		pwmc.Init_PWM(GPIOA, GPIO_Pin_2, TIM2, 3);
//		pwmc.Output(0.4f);

		char data_1[] = "I Love U 1";
		char data_2[] = "I Fuck U 2";
		char data_3[] = "I Fuck U 3";
		uint16_t ii = 0;
		while (1)
		{
				Resv_Data_test_1();
				Resv_Data_test_2();
//				Resv_Data_test_3();
				
				if(ii++ > 1000)
				{
						ii = 0;
						serial_1.SendByte('a');
						serial_2.SendByte('b');
//						serial_3.SendByte('c');
//						vTaskDelay(1);
						serial_1.SendData(data_1, strlen(data_1));
						serial_2.SendData(data_2, strlen(data_2));
//						serial_3.SendData(data_3, strlen(data_3));
				}
				vTaskDelay(1);
		}     

}

void vSystemNormalLED(void *pvArg) 
{
		My_GPIO led(GPIOC, GPIO_Pin_13);
	
		while (1)
		{
				led.Reset();	
				vTaskDelay(10);
				led.Set();	
				vTaskDelay(910);
		}     

}
